import type { ResponseData } from '@/utils/types.d';
import { SUCCESS_CODE } from '@/utils/types.d';
import request from '@/utils/request';
import type { BatchTaskDetailTableItem, BatchTaskTableItem, CreateTaskType } from './data';

export async function queryBatchTasks(params: any, sort: any) {
  const res: ResponseData<{
    total: number;
    data: BatchTaskTableItem[];
  }> = await request('/api/batch-task/v1.0.0/tasks', {
    params: {
      data: params,
      sort,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

export async function queryBatchTaskDetails(params: any, sort: any) {
  const { taskId } = params;
  const res: ResponseData<{
    total: number;
    data: BatchTaskDetailTableItem[];
  }> = await request(`/api/batch-task/v1.0.0/task-details/${taskId}`, {
    params: {
      data: params,
      sort,
    },
  });
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

export async function getDownloadTemplateFile(type: string) {
  return await request('/api/batch-task/v1.0.0/template-file', {
    params: { type },
    responseType: 'blob',
  });
}

export async function createTask(data: CreateTaskType) {
  const res: ResponseData<null> = await request.post('/api/batch-task/v1.0.0/task', {
    data,
  });
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: '添加成功',
    };
  return {
    data: res?.data,
    success: false,
    message: res?.message || '添加失败',
  };
}

/* export async function exportTaskDetail(taskId: string) {
  return await request(`/api/batch-task/v2.0.0/detail-file/${taskId}`, {
    responseType: 'blob',
  });
} */

export async function uploadTaskPreFile(form: FormData) {
  const res = await request('/api/batch-task/v1.0.0/pre-file', {
    method: 'POST',
    requestType: 'form',
    data: form,
    body: form,
  });
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: res.message || '上传成功',
    };
  return { data: res?.data, success: false, message: res?.message || '上传失败' };
}
